'use strict';

const Controller = require('egg').Controller;
class groupTree extends Controller {
    //左侧树形结构
    async getTree() {
        const { app, ctx } = this;
        const { Raw } = app.Dbs.tree;
        const treeData = await Raw.QueryList('select groupdata.id,groupdata.sort,groupdata.label,groupdata.pid,groupdata.icon,groupdata.expand,groupdata.roleKey,groupdata.classify from groupdata union select userdata.id,userdata.sort,tabledata.label,userdata.pid,userdata.icon,userdata.expand,userdata.roleKey,userdata.classify from tabledata INNER JOIN userdata ON userdata.tid = tabledata.id order by classify,sort')
        // return treeData
        ctx.body = ctx.service.tree.buildTree(treeData);
    }
    //部门信息
    async searchGroup() {
        const { app, ctx } = this;
        // let { id } = ctx.query;
        const { Raw } = app.Dbs.tree;
        const search = await Raw.QueryList('select * from groupdata')
        ctx.body = ctx.service.tree.buildTree(search);
    }
    //点击某部门的数据
    async searchGroupMsg() {
        const { app, ctx } = this;
        let { id } = ctx.query;
        const { Raw } = app.Dbs.tree;
        const search = await Raw.QueryList('select * from groupdata  where id= :id', {
            replacements: { id }
        })
        return search
    }
    //部门排序信息
    async searchGSortMsg() {
        const { app, ctx } = this;
        let { pid } = ctx.query;
        const { Raw } = app.Dbs.tree;
        const search = await Raw.QueryList('select * from groupdata  where pid= :pid order by sort', {
            replacements: { pid }
        })
        return search
    }
    //部门排序提交
    async postGSort() {
        const { app, ctx } = this;
        let { id, sort } = ctx.request.body
        let data = ctx.request.body
        const { Raw } = app.Dbs.tree;
        await Raw.Update('groupdata', data, {
            wherestr: 'where id=:id',
            replacements: { id }
        })
    }
    //删除部门
    async deleteGroup() {
        const { app, ctx } = this;
        let { id } = ctx.query;
        const { Raw } = app.Dbs.tree;
        const search = await Raw.Delete('groupdata', {
            wherestr: "where id=:id",
            replacements: { id }
        })
        return search
    }
    //用户排序信息
    async searchUSortMsg() {
        const { app, ctx } = this;
        let { pid } = ctx.query;
        const { Raw } = app.Dbs.tree;
        //where pid= :pid
        const search = await Raw.QueryList('select tabledata.label,userdata.pid,userdata.id,userdata.classify,userdata.sort from tabledata INNER JOIN userdata ON userdata.tid = tabledata.id where userdata.pid= :pid  order by userdata.sort', {
            replacements: { pid }
        })
        return search
    }
    //用户排序提交
    async postUSort() {
        const { app, ctx } = this;
        let { id, sort } = ctx.request.body
        let data = ctx.request.body
        const { Raw } = app.Dbs.tree;
        await Raw.Update('userdata', data, {
            wherestr: 'where id=:id',
            replacements: { id }
        })
    }
    //用户信息
    async getUser() {
        const { app, ctx } = this;
        const { id } = ctx.query;
        const { Raw } = app.Dbs.tree;
        const Data = await Raw.Execute('select tabledata.*,userdata.* from tabledata right join userdata on userdata.tid=tabledata.id where userdata.id=:id', {
            replacements: { id }
        })
        return Data
    }
    //删除用户
    async deleteUser() {
        const { app, ctx } = this;
        let { id } = ctx.query;
        const { Raw } = app.Dbs.tree;
        await Raw.ExecuteNoQuery('delete from userdata where id=:id', {
            replacements: { id }
        })
    }
    //新增部门
    async addGroup() {
        const { app, ctx } = this;
        let data = ctx.request.body;
        // ctx.body=data
        const { Raw } = app.Dbs.tree;
        const search = await Raw.Insert('groupdata', data)
        return search
    }
    //更新部门信息
    async updateGroup() {
        const { app, ctx } = this;
        let data = ctx.request.body;
        let { id } = ctx.request.body;
        const { Raw } = app.Dbs.tree;
        const search = await Raw.UpdateOrInsert('groupdata', data, {
            wherestr: "where id=:id",
            replacements: { id }
        })
        return search
    }
    //新增用户
    async addUser() {
        const { app, ctx } = this;
        let { userName, password, label, pid, status, sex, phone, email, allPid } = ctx.request.body;
        const { Raw } = app.Dbs.tree;
        await Raw.ExecuteNoQuery('insert into tabledata (userName, password, label, status, sex, phone, email, allPid) value (:userName,:password, :label, :status, :sex, :phone, :email, :allPid)', {
            replacements: { userName, password, label, status, sex, phone, email, allPid },
        })
        let result = await Raw.Query('select MAX(sort) from userdata')
        let max = result['MAX(sort)'] + 1
        await Raw.ExecuteNoQuery('insert into userdata (tid,pid,sort) value (LAST_INSERT_ID(),:pid,:max)', {
            replacements: { pid, max },
        })
    }
    //更新用户信息
    async updateUser() {
        const { app, ctx } = this;
        let { id, tid, userName, password, label, pid, status, sex, phone, email, allPid } = ctx.request.body;
        const { Raw } = app.Dbs.tree;
        let result = await Raw.Query(`select count(1) as 'c' from tabledata right join userdata on userdata.tid=tabledata.id where label=:label AND pid=:pid`, {
            replacements: { label, pid }
        })
        if (result.c == 0) {
            console.log('不存在')
            await Raw.ExecuteNoQuery('update tabledata set userName=:userName,password=:password,label=:label,status=:status,sex=:sex,phone=:phone,email=:email,allPid=:allPid where tabledata.id=:tid ', {
                replacements: { tid, userName, password, label, status, sex, phone, email, allPid }
            })
            let result = await Raw.Query('select MAX(sort) from userdata')
            let max = result['MAX(sort)'] + 1
            await Raw.ExecuteNoQuery('insert into userdata (tid,pid,sort) value (:tid,:pid,:max)', {
                replacements: { tid, pid, max },
            })
        }
        else {
            console.log('请修改');
            await Raw.ExecuteNoQuery('update tabledata right join userdata on userdata.tid=tabledata.id set userName=:userName,password=:password,label=:label,pid=:pid,status=:status,sex=:sex,phone=:phone,email=:email,allPid=:allPid where userdata.id=:id ', {
                replacements: { id, userName, password, label, pid, status, sex, phone, email, allPid }
            })
        }
    }
    async hhh() {
        const { app, ctx } = this;
        const { tid, pid } = ctx.request.body;
        const { Raw } = app.Dbs.tree;
        let result = await Raw.Query('select MAX(sort) from userdata')
        let max = result['MAX(sort)'] + 1
        // return max
        await Raw.ExecuteNoQuery('insert into userdata (tid,pid,sort) values (:tid,:pid,:max)', {
            replacements: { tid, pid, max }
        })
    }

}
module.exports = groupTree;